package com.wjt.homestayms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjt.homestayms.mapper.RoomMapper;
import com.wjt.homestayms.mapper.StoreMapper;
import com.wjt.homestayms.common.PageRequest;
import com.wjt.homestayms.common.PageResult;
import com.wjt.homestayms.pojo.Room;
import com.wjt.homestayms.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class RoomServiceImpl implements IRoomService {

    @Autowired
    RoomMapper roomMapper;

    @Autowired
    StoreMapper storeMapper;

    @Override
    public boolean addRoom(Room room) {
        //先根据房间号查询，是否有相同的房间号，如果有，不给增加。没有,便执行增加功能。
        List<Room> roomList = roomMapper.getRoomByRnum(room.getrNum());
        if(roomList.size() > 0){
            return false;
        }else{
            Integer row = roomMapper.addRoom(room);
            //大于0表示，增加房间成功。
            if(row > 0){
                return true;
            }else{
                return false;
            }
        }

    }

    @Override
    public boolean updateRoom(Room room) {
        //先查询是否有信息，如果有，则修改，没有，则不执行修改。
        List<Room> roomList = roomMapper.getRoomByRid(room.getrId());
        if(roomList.size() > 0){
            boolean flag = roomMapper.updateRoomInfo(room);
            return flag;
        }
        return false;
    }

    @Override
    public boolean modifyStatus(Integer rId, String status) {
        //先查询是否有信息，如果有，则修改状态，没有，则不执行修改。
        List<Room> roomList = roomMapper.getRoomByRid(rId);
        if(roomList.size()>0){
            boolean flag = roomMapper.modifyStatus(rId, status);
            return flag;
        }
        return false;
    }

    @Override
    public boolean deleteRoomByRid(Integer rId) {
        //先查询是否有信息，如果有，就删除，没有，则不执行删除。
        List<Room> roomList = roomMapper.getRoomByRid(rId);
        if(roomList.size()>0){
            Integer row = roomMapper.deleteRoomByRid(rId);
            if(row > 0){
                return true;
            }else {
                return false;
            }
        }
        return false;
    }

    @Override
    public PageResult getResultPage(PageRequest pageRequest, Integer id) {
        PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize());
        List<Room> roomList = roomMapper.getRoomList(id);
        return getPageResult(roomList);
    }

    @Override
    public PageResult getResultPageByRnum(PageRequest pageRequest, Integer rNum) {
        PageHelper.startPage(pageRequest.getPageNum(),pageRequest.getPageSize());
        List<Room> roomList = roomMapper.getRoomByRnum(rNum);
        return getPageResult(roomList);
    }

    private PageResult getPageResult(List<Room> list) {
        //传入的list长度大于0，表示list中有数据存在。
        if (list.size() > 0) {
            PageInfo<Room> pageInfo = PageInfo.of(list);
            PageResult page = new PageResult();
            //获取数据记录总数
            page.setTotal(pageInfo.getTotal());
            //获取当前页面页码
            page.setPageNum(pageInfo.getPageNum());
            //获取当前页面数据的个数
            page.setPageSize(pageInfo.getPageSize());
            //获取页面总数
            page.setTotalPage(pageInfo.getPages());
            //封装页面内容
            Iterator<Room> iterator = pageInfo.getList().iterator();
            List<Object> content = new ArrayList<>();
            while (iterator.hasNext()){
                Room room = iterator.next();
                //Sid转换成Sname。
                room.setsName(storeMapper.getStoreName(room.getsId()));
                content.add(room);
            }

            page.setContent(content);
            //封装完成返回PageResult对象
            return page;
        } else {
            return null;
        }
    }

    @Override
    public Integer getSidByRnum(Integer rNum) {
        Integer storeId = roomMapper.getStoreId(rNum);
        return storeId;
    }

    @Override
    public Room getRoomById(Integer rId) {
        List<Room> roomList = roomMapper.getRoomByRid(rId);
        Room room = null;
        if(roomList !=null){
            Iterator<Room> iterator = roomList.iterator();
            if(iterator.hasNext()){
                room = iterator.next();
            }
        }
        return room;
    }
}
